﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;
using System.Web.UI.HtmlControls;

public partial class addArchitects : System.Web.UI.Page
{
    #region Variable Declaration

    private IDataRepository _dataObject = new DataRepository();
    private Architect architect;
    private User user;
    private List<Architect> architects = new List<Architect>();
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["User"] == null)
            Response.Redirect("default.aspx");
        else
        {
            if (Request.QueryString["aId"] != null)
                updateUserArchitect(Request.QueryString["aId"].ToString());
            BindArchitectsGrid();
        }
    }
    private void updateUserArchitect(string architectId)
    {
        User nUser = new User(_dataObject);
        nUser.UserName = Session["User"].ToString();
        nUser.GetUserByUserName();

        if (nUser.Id != 0)
        {
            user = new User(_dataObject);
            user.Id = nUser.Id;
            user.ArchitectId = Convert.ToInt32(architectId);
            user.UpdateArchitectId();
        }
    }
    private void BindArchitectsGrid()
    {
        architect = new Architect(_dataObject);
        architects = architect.GetAllArchitects();
        if (architects.Count > 0)
        {
            spMessage.Text = string.Empty;
            dListArchitects.DataSource = architects;
            dListArchitects.DataBind();
        }
        else
        {
            spMessage.Text = "No Architects added.";
            dListArchitects.DataSource = null;
            dListArchitects.DataBind();
        }
    }
    protected void dListArchitects_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HtmlGenericControl spDateCreated = (HtmlGenericControl)e.Item.FindControl("spDateCreated");
            Label spanDate = (Label)e.Item.FindControl("spanDate");

            spDateCreated.InnerHtml = "<small>" +
                                      ((CDB.LinqModel.CDBGeneral.Month)Convert.ToDateTime(spanDate.Text).Month).
                                          ToString() + "</small>";
            spDateCreated.InnerHtml += "<div>" +
                                       (Convert.ToDateTime(spanDate.Text).Day).
                                           ToString() + "</div>";

        }
    }
}